package org.idol.generator.plugin;

import org.idol.generator.tools.IntrospectedTableTools;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Properties;

/**
 * @author coco
 * @date 2020-09-24 09:49
 **/
public class AutoFillPlugin extends PluginAdapter {

    private static final String HANDLER = "handler";

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field,
                                       TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable,
                                       ModelClassType modelClassType) {

        // 1. 获取插件的配置
        Properties properties = getProperties();
        // 2. 获取配置的逻辑删除列  先从plugin中获取配置 table的配置的会覆盖plugin对应字段
        String handlerClass = properties.getProperty(HANDLER);

        if (introspectedTable.getTableConfigurationProperty(HANDLER) != null) {
            handlerClass = introspectedTable.getTableConfigurationProperty(HANDLER);
        }
        if (properties.keySet().contains(introspectedColumn.getActualColumnName())) {
            topLevelClass.addImportedType(new FullyQualifiedJavaType("com.idol.boost.annotation.AutoFilled"));
            String handlerClassName = null;
            if (StringUtility.stringHasValue(handlerClass)) {
                handlerClassName = handlerClass.substring(handlerClass.lastIndexOf(".")+1,handlerClass.length());
                topLevelClass.addImportedType(new FullyQualifiedJavaType(handlerClass));
            }

            String property = properties.getProperty(introspectedColumn.getActualColumnName());
            StringBuffer sb = new StringBuffer();
            if (StringUtility.stringHasValue(property)) {
                topLevelClass.addImportedType(new FullyQualifiedJavaType("com.idol.boost.core.enums.FilledType"));
                sb.append("@AutoFilled(fill={");
                String[] split = property.trim().split(",");
                for (String s : split) {
                    if ("insert".equals(s)) {
                        sb.append("FilledType.INSERT,");
                    } else if ("update".equals(s)) {
                        sb.append("FilledType.UPDATE,");
                    }
                }
                String substring = sb.substring(0, sb.length() - 1);
                sb.setLength(0);
                sb.append(substring);
                sb.append("}");
                if (StringUtility.stringHasValue(handlerClass)){
                    sb.append(",");
                    topLevelClass.addImportedType(new FullyQualifiedJavaType(handlerClass));
                    sb.append("fillClass=");
                    sb.append(handlerClassName+".class");
                }
                sb.append(")");
            } else {
                sb.append("@AutoFilled(");
                if (StringUtility.stringHasValue(handlerClass)){
                    topLevelClass.addImportedType(new FullyQualifiedJavaType(handlerClass));
                    sb.append("fillClass=");
                    sb.append(handlerClassName+".class");

                }
                sb.append(")");
            }
            field.addAnnotation(sb.toString());

        }
        return true;
    }
}
